package com.ogawa.widget;

import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.ogawa.base.R;
import com.xuexiang.xfloatview.XFloatView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


/**
  * 采集数据
 */
public class DataShowPopWindow extends XFloatView {
    private TextView deviceInfoTv;
    private TextView debugInfoTv;
    // 上下文
    private Context mContext;
     public DataShowPopWindow(Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.layout_data_show_popwindow;
    }

    //能否移动或者触摸响应
    @Override
    protected boolean canMoveOrTouch() {
        return true;
    }


    //初始化悬浮控件
    @Override
    protected void initFloatView() {
        EventBus.getDefault().register(this);
     }
    //初始化监听
    @Override
    protected void initListener() {

    }
    //设置悬浮框是否吸附在屏幕边缘
    @Override
    protected boolean isAdsorbView() {
        return true;
    }

    @Override
    public void clear() {
        super.clear();
        EventBus.getDefault().unregister(this);

    }

    /**
     * 初始化
     */
    private void init() {
        findViewById(R.id.closeBt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                dismiss();
                getWmParams().height = 300;
                show();
            }
        });
        findViewById(R.id.bigBt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getWmParams().height = WindowManager.LayoutParams.MATCH_PARENT;
                show();
            }
        });
        deviceInfoTv = findViewById(R.id.deviceInfoTv);
        debugInfoTv = findViewById(R.id.debugInfoTv);
        findViewById(R.id.clearDebugInfoBt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                debugInfoTv.setText("");
            }
        });
     }

     @Subscribe(threadMode = ThreadMode.MAIN)
     public void receiveSendDataPopWindowDebugInfo(DataShowPopWindowDebugInfoBean dataShowPopWindowDebugInfoBean){
        try{
            debugInfoTv.append(dataShowPopWindowDebugInfoBean.info);
            debugInfoTv.append("\r\n");
        }catch (Exception e){
            e.printStackTrace();
        }
     }

    @Subscribe
    public void receiveDataShowPopWindowBean(DataShowPopWindowBean dataShowPopWindowBean){
        try{
            deviceInfoTv.setText(dataShowPopWindowBean.info);
        }catch (Exception e){
            e.printStackTrace();
        }

    }


     
    public static class DataShowPopWindowBean{
        String info;

        public DataShowPopWindowBean(String info) {
            this.info = info;
        }
    }
    public static class DataShowPopWindowDebugInfoBean{
        String info;

        public DataShowPopWindowDebugInfoBean(String info) {
            this.info = info;
        }
    }

}
